package org.example.clouddemo.service.common.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.example.clouddemo.bean.common.entity.Order;
import org.example.clouddemo.bean.common.entity.Stock;
import org.example.clouddemo.bean.common.entity.UserWallet;


import org.example.clouddemo.bean.common.param.PayOrderParam;
import org.example.clouddemo.bean.common.param.StockParam;
import org.example.clouddemo.feign.StockFeignClient;
import org.example.clouddemo.mapper.OrderMapper;
import org.example.clouddemo.mapper.UserWalletMapper;

import org.example.clouddemo.service.common.PaymentService;
import org.example.common.exception.AppRunTimeException;
import org.example.common.response.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;


@Service
public class PaymentServiceImpl extends ServiceImpl<UserWalletMapper, UserWallet> implements PaymentService {

    @Autowired
    private UserWalletMapper userWalletMapper;

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    public StockFeignClient stockFeignClient;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean pay(Order order, BigDecimal amount) {
        UserWallet userWallet = userWalletMapper.selectById(order.getUserId());
        if (userWallet == null) {
            throw new AppRunTimeException("用户钱包不存在");
        }
        if (userWallet.getBalance().compareTo(amount) < 0) {
            throw new AppRunTimeException("余额不足");
        }
//        // 5. 扣减库存
//        stock.setQuantity(stock.getQuantity() - quantity);
//        stockFeignClient.update(stock);
        userWallet.setBalance(userWallet.getBalance().subtract(amount));
        if (userWalletMapper.updateById(userWallet) > 0) {
            // 支付成功，修改订单状态
            order.setStatus(2); // 2-已支付
            // 这里需要注入OrderMapper来更新订单状态
            orderMapper.updateById(order);
            return true;
        }
        return false;
    }

    @Override
    public boolean payOrder(PayOrderParam param) {
        UserWallet userWallet = userWalletMapper.selectById(param.getUserId());
        if (userWallet == null) {
            throw new AppRunTimeException("用户钱包不存在");
        }
        if (userWallet.getBalance().compareTo(param.getAmount()) < 0) {
            throw new AppRunTimeException("余额不足");
        }
        // 5. 扣减库存
        StockParam stockParam = new StockParam(param.getProductId(), 0);
        ApiResult<Stock> result = stockFeignClient.getOne(stockParam);
        Stock stock = result.getData();
        stock.setQuantity(stock.getQuantity() - param.getQuantity());
        stockFeignClient.update(stock);
        userWallet.setBalance(userWallet.getBalance().subtract(param.getAmount()));
        Order order = orderMapper.selectById(param.getOrderId());
        if (order == null) {
            throw new AppRunTimeException("订单不存在");
        }
        if (userWalletMapper.updateById(userWallet) > 0) {
            // 支付成功，修改订单状态
            order.setStatus(2); // 2-已支付
            // 这里需要注入OrderMapper来更新订单状态
            orderMapper.updateById(order);
            return true;
        }
        return false;
    }
}